package com.nzw.common.service.wx;


import com.nzw.utils.MD5Utils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created by XuZhansi on 2017/8/16.
 */
public class WXPlatform {
    private String validToken;//公众号消息校验Token
    private String encodingAesKey;//公众号消息加解密Key
    private String payMchid;//微信支付商户号
    private String paySecret;//支付密钥
    private byte[] certData;//支付ssl证书
    private String publicAppid;//公众号appid
    private String publicSecret;//公众号的密钥

    public String getPublicSecret() {
        return publicSecret;
    }

    public void setPublicSecret(String publicSecret) {
        this.publicSecret = publicSecret;
    }

    public String getPublicAppid() {
        return publicAppid;
    }

    public void setPublicAppid(String publicAppid) {
        this.publicAppid = publicAppid;
    }

    public WXPlatform(){}

    public byte[] getCertData() {
        return certData;
    }

    public void setCertData(byte[] certData) {
        this.certData = certData;
    }

    public String getPayMchid() {
        return payMchid;
    }

    public void setPayMchid(String payMchid) {
        this.payMchid = payMchid;
    }

    public String getPaySecret() {
        return paySecret;
    }

    public void setPaySecret(String paySecret) {
        this.paySecret = paySecret;
    }


    public String getValidToken() {
        return validToken;
    }

    public void setValidToken(String validToken) {
        this.validToken = validToken;
    }

    public String getEncodingAesKey() {
        return encodingAesKey;
    }

    public void setEncodingAesKey(String encodingAesKey) {
        this.encodingAesKey = encodingAesKey;
    }

    public String createPaySign(Map<String,String> params) {
        List<String> elements = new ArrayList<>(params.keySet());
        Collections.sort(elements, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        String signTemp = "";
        for (String e : elements) {
            signTemp += "&" + e + "=" + params.get(e);
        }
        signTemp = signTemp.substring(1);
        String stringSignTemp = signTemp + "&key=" + getPaySecret(); //注：key为商户平台设置的密钥key
        String sign = MD5Utils.getMD5Code(stringSignTemp).toUpperCase();//注：MD5签名方式
        return sign;
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }
}
